package com.game.modular.game.act.battle;

import com.game.comment.core.ResponseResult;
import com.game.comment.util.JwtUtils;
import com.game.modular.game.act.battle.vo.BattleVo;
import com.game.modular.game.battle.info.base.PvpPlayerInfoServiceImpl;
import com.game.modular.game.match.service.PvpMatchEndServiceImpl;
import com.game.modular.game.playerInfo.domain.Player;
import com.game.modular.game.playerInfo.domain.PlayerMatch;
import com.game.modular.game.playerInfo.service.PlayerServiceImpl;
import com.game.modular.game.playerMatch.domain.PvpMatch;
import com.game.modular.game.playerMatch.service.PlayerMatchServiceImpl;
import com.game.modular.game.match.service.PvpMatchServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;


@Api(tags = "pvp匹配")
@Validated
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/pvpMatchAct")
public class PvpMatchAction {
    @Resource
    private PvpMatchServiceImpl pvpMatchService;    @Resource
    private PvpMatchEndServiceImpl pvpMatchEndService;
    @Resource
    private PvpPlayerInfoServiceImpl pvpPlayerInfoServiceImpl;
    @Resource
    private PlayerMatchServiceImpl playerMatchServiceImpl;
    @Resource
    private PlayerServiceImpl playerServiceImpl;
    @Resource
    private PvpMatchServiceImpl pvpMatchServiceImpl;

    @ApiOperation(value = "查询比赛信息")
    @PostMapping("/select")
    public ResponseResult<PvpMatch> select(
            @Valid @RequestBody BattleVo.pvpMatchVo.select param
    ) {
        return ResponseResult.success("成功匹配对局",
                pvpMatchService.select(param.getMatchId())
        );
    }

    @ApiOperation(value = "获取匹配")
    @PostMapping("/getMatch")
    public ResponseResult<PvpMatch> getMatch(
            @RequestHeader String token,
            @Valid @RequestBody BattleVo.pvpMatchVo.GetMatchVo param
    ) {
        Long playerId = JwtUtils.getPlayerId(token);
        Integer mapId = param.getMapId();
        Integer type = param.getType();
        Integer startThreshold = param.getStartThreshold();
        return ResponseResult.success("成功匹配对局",
                pvpMatchService.match(playerId, mapId, type, startThreshold)
        );
    }

    @ApiOperation(value = "取消匹配")
    @PostMapping("/cancelMatch")
    public ResponseResult<PvpMatch> cancelMatch(
            @RequestHeader String token
    ) {
        return ResponseResult.success("成功取消对局", pvpMatchService.cancelMatch(
                JwtUtils.getPlayerId(token))
        );
    }

    @ApiOperation(value = "投降")
    @PostMapping("/exitGame")
    public ResponseResult<?> exitGame(
            @RequestHeader String token
    ) {
        Long playerId = JwtUtils.getPlayerId(token);
        Long currentMatchId = playerMatchServiceImpl.currentMatchId(playerId);
        pvpMatchEndService.exitGame(playerId, currentMatchId);
        return ResponseResult.success("成功退出对局");
    }

    @ApiOperation(value = "参加对局的玩家")
    @PostMapping("/allPlayers")
    public ResponseResult<List<Player>> playersParticipatingInTheGame(
            @RequestHeader String token
    ) {
        Long playerId = JwtUtils.getPlayerId(token);
        Long currentMatchId = playerMatchServiceImpl.currentMatchId(playerId);
        PvpMatch byId = pvpMatchServiceImpl.getById(currentMatchId);
        List<Player> collect =
                byId.getPlayerCount().stream().map(item -> playerServiceImpl.select(item)).collect(Collectors.toList());
        return ResponseResult.success("成功参加对局的玩家", collect);
    }

    @ApiOperation(value = "当前对局存活的玩家")
    @PostMapping("/survivingPlayers")
    public ResponseResult<List<Player>> currentSurvivingPlayersInTheGame(
            @RequestHeader String token
    ) {
        Long playerId = JwtUtils.getPlayerId(token);
        PlayerMatch playerMatch = playerMatchServiceImpl.selectCache(playerId);
        PvpMatch byId = pvpMatchServiceImpl.getById(playerMatch.getCurrentMatchId());
        Integer status = byId.getStatus();
        if (status!=1){
            throw new RuntimeException("当前对局未进行");
        }
        List<Player> collect =
                byId.getPlayerIds().stream().map(item -> playerServiceImpl.select(item)).collect(Collectors.toList());
        return ResponseResult.success("成功当前对局存活的玩家", collect);
    }
}
